package com.asura.app.wechat.ui.moment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.asura.app.wechat.R;
import com.asura.app.wechat.base.AutoLoadMoreAdapterWrapper;
import com.asura.app.wechat.base.BaseMvpActivity;
import com.asura.app.wechat.repository.model.Tweet;
import com.asura.app.wechat.repository.model.UserInfo;
import com.asura.app.wechat.utils.UIUtils;
import com.asura.app.wechat.widget.WechatPullToRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class MomentActivity extends BaseMvpActivity<MomentContract.Presenter> implements MomentContract.View {


    @BindView(R.id.rv_moment)
    RecyclerView rvMoment;
    @BindView(R.id.refresh_layout)
    WechatPullToRefreshLayout refreshLayout;
    private TweetListAdapter tweetListAdapter;
    private AutoLoadMoreAdapterWrapper autoLoadMoreAdapterWrapper;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_moment;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        super.init(savedInstanceState);
        rvMoment.setOverScrollMode(View.OVER_SCROLL_NEVER);
        rvMoment.setHasFixedSize(true);
        rvMoment.setLayoutManager(new LinearLayoutManager(MomentActivity.this, LinearLayoutManager.VERTICAL, false));
        rvMoment.addItemDecoration(new TweetListDividerItemDecoration(this));
        tweetListAdapter = new TweetListAdapter(MomentActivity.this, null);
        autoLoadMoreAdapterWrapper = new AutoLoadMoreAdapterWrapper(this, tweetListAdapter);
        autoLoadMoreAdapterWrapper.setAutoLoadMoreThreshold(3);
        autoLoadMoreAdapterWrapper.setOnLoadMoreListener(new AutoLoadMoreAdapterWrapper.OnAutoLoadMoreListener() {
            @Override
            public void onAutoLoadMore() {
                Tweet last = (Tweet) (tweetListAdapter.getData().get(tweetListAdapter.getData().size() - 1));
                getPresenter().loadMoreTweets(last.getId());
                refreshLayout.setEnabled(false);
            }
        });
        refreshLayout.setOnRefreshListener(new WechatPullToRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getPresenter().refreshTweets();
                autoLoadMoreAdapterWrapper.setAutoLoadMoreEnable(false);
            }

            @Override
            public void cancelRefresh() {
                getPresenter().cancelRefreshTweets();
            }
        });
        rvMoment.setAdapter(autoLoadMoreAdapterWrapper);
        getPresenter().initLoadTweets();
    }

    @Override
    protected MomentContract.Presenter createPresenter() {
        return new MomentPresenter();
    }


    @Override
    public void showInitLoadLoading() {
        refreshLayout.setEnabled(false);
        autoLoadMoreAdapterWrapper.setAutoLoadMoreEnable(false);
        tweetListAdapter.setInitLoading(true);
        ArrayList<Object> data = new ArrayList<>(2);
        data.add(new UserInfo());
        data.add(new Object());
        tweetListAdapter.updateDiff(data);
    }

    @Override
    public void showInitLoadResult(UserInfo userInfo, List<Tweet> tweets) {
        refreshLayout.setEnabled(true);
        autoLoadMoreAdapterWrapper.setAutoLoadMoreEnable(true);
        tweetListAdapter.setInitLoading(false);
        List<Object> data = new ArrayList<>(tweets.size() + 1);
        data.add(userInfo);
        data.addAll(tweets);
        tweetListAdapter.updateDiff(data);
    }

    @Override
    public void showInitLoadError(String message) {
        refreshLayout.setEnabled(true);
        autoLoadMoreAdapterWrapper.setAutoLoadMoreEnable(true);
        UIUtils.showToast(message);
    }

    @Override
    public void showLoadMoreLoading() {

    }

    @Override
    public void showLoadMoreResult(List<Tweet> tweets) {
        refreshLayout.setEnabled(true);
        autoLoadMoreAdapterWrapper.setAutoLoadMoreFinish();
        if(tweets.isEmpty()) {
            //up to end
            autoLoadMoreAdapterWrapper.setAutoLoadMoreEnable(false);
            return;
        }
        ArrayList<Object> data = new ArrayList<>(tweets);
        tweetListAdapter.addAll(data);
    }

    @Override
    public void showLoadMoreError(String message) {
        autoLoadMoreAdapterWrapper.setAutoLoadMoreFinish();
        refreshLayout.setEnabled(true);
        UIUtils.showToast(message);
    }

    @Override
    public void showRefreshLoading() {

    }

    @Override
    public void showRefreshResult(UserInfo userInfo, List<Tweet> tweets) {
        refreshLayout.setRefreshing(false);
        autoLoadMoreAdapterWrapper.setAutoLoadMoreEnable(true);
        List<Object> data = new ArrayList<>(tweets.size() + 1);
        data.add(userInfo);
        data.addAll(tweets);
        tweetListAdapter.updateDiff(data);
    }

    @Override
    public void showRefreshError(String message) {
        refreshLayout.setRefreshing(false);
        autoLoadMoreAdapterWrapper.setAutoLoadMoreEnable(true);
        UIUtils.showToast(message);
    }
}
